import js from '@eslint/js'
import vue from 'eslint-plugin-vue'
import prettier from 'eslint-plugin-prettier'
import typescript from '@typescript-eslint/eslint-plugin'
import typescriptParser from '@typescript-eslint/parser'

export default [
  // 基础JavaScript规则
  js.configs.recommended,

  // Vue文件配置
  ...vue.configs['flat/recommended'],

  {
    files: ['**/*.{js,mjs,cjs,vue}'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      globals: {
        // 浏览器环境
        window: 'readonly',
        document: 'readonly',
        navigator: 'readonly',
        console: 'readonly',
        // Node.js环境
        process: 'readonly',
        __dirname: 'readonly',
        __filename: 'readonly',
        // 浏览器 Web APIs
        localStorage: 'readonly',
        sessionStorage: 'readonly',
        fetch: 'readonly',
        FormData: 'readonly',
        URLSearchParams: 'readonly',
        AbortController: 'readonly',
        setTimeout: 'readonly',
        clearTimeout: 'readonly',
        setInterval: 'readonly',
        clearInterval: 'readonly',
        // 自动引入的全局变量
        ref: 'readonly',
        reactive: 'readonly',
        computed: 'readonly',
        watch: 'readonly',
        watchEffect: 'readonly',
        onMounted: 'readonly',
        onUnmounted: 'readonly',
        onBeforeMount: 'readonly',
        onBeforeUnmount: 'readonly',
        onUpdated: 'readonly',
        onBeforeUpdate: 'readonly',
        nextTick: 'readonly',
        useRouter: 'readonly',
        useRoute: 'readonly',
        defineStore: 'readonly',
        createRouter: 'readonly',
        createWebHistory: 'readonly',
        ElMessage: 'readonly',
        ElMessageBox: 'readonly',
        ElNotification: 'readonly',
        ElLoading: 'readonly',
        debounce: 'readonly',
        throttle: 'readonly',
        cloneDeep: 'readonly',
        merge: 'readonly',
        omit: 'readonly',
        pick: 'readonly',
        uniq: 'readonly',
        isEmpty: 'readonly',
        isEqual: 'readonly',
        nanoid: 'readonly',
        // Element Plus 图标 (自动引入)
        Monitor: 'readonly',
        Setting: 'readonly',
        Cpu: 'readonly',
        DataBoard: 'readonly',
        Connection: 'readonly',
        Tools: 'readonly',
        House: 'readonly',
        InfoFilled: 'readonly',
        UserFilled: 'readonly',
        Moon: 'readonly',
        Sunny: 'readonly',
        MagicStick: 'readonly',
        Plus: 'readonly',
        Refresh: 'readonly',
        Upload: 'readonly',
        Download: 'readonly',
        ElementPlus: 'readonly',
        // Composables (自动引入)
        useApi: 'readonly',
        useUserApi: 'readonly',
        useArticleApi: 'readonly'
      }
    },
    plugins: {
      prettier
    },
    rules: {
      // Prettier集成
      'prettier/prettier': 'error',

      // JavaScript规则
      'no-console': 'warn',
      'no-debugger': 'warn',
      'no-unused-vars': [
        'error',
        {
          argsIgnorePattern: '^_',
          varsIgnorePattern: '^_'
        }
      ],
      'prefer-const': 'error',
      'no-var': 'error',

      // Vue特定规则
      'vue/multi-word-component-names': 'off',
      'vue/no-unused-vars': 'error',
      'vue/no-unused-components': 'warn',
      'vue/order-in-components': 'error',
      'vue/attributes-order': 'error',
      'vue/block-order': [
        'error',
        {
          order: ['template', 'script', 'style']
        }
      ],
      'vue/html-self-closing': 'off',
      'vue/max-attributes-per-line': 'off',
      'vue/first-attribute-linebreak': 'off',
      'vue/singleline-html-element-content-newline': 'off',
      'vue/html-indent': 'off',
      'vue/html-closing-bracket-newline': 'off',

      // 代码质量
      eqeqeq: ['error', 'always'],
      curly: ['error', 'all'],
      'brace-style': ['error', '1tbs'],
      'comma-dangle': 'off',
      semi: ['error', 'never'],
      quotes: ['error', 'single'],
      'object-curly-spacing': ['error', 'always'],
      'array-bracket-spacing': ['error', 'never']
    }
  },

  // TypeScript文件特殊配置
  {
    files: ['**/*.{ts,tsx}'],
    languageOptions: {
      parser: typescriptParser,
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module'
      }
    },
    plugins: {
      '@typescript-eslint': typescript,
      prettier
    },
    rules: {
      // 继承基础规则
      ...js.configs.recommended.rules,
      'prettier/prettier': 'error',

      // TypeScript特定规则
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          argsIgnorePattern: '^_',
          varsIgnorePattern: '^_'
        }
      ],
      '@typescript-eslint/explicit-function-return-type': 'off',
      '@typescript-eslint/explicit-module-boundary-types': 'off',
      '@typescript-eslint/no-explicit-any': 'warn',
      'no-unused-vars': 'off' // 关闭基础规则，使用TypeScript版本
    }
  },

  // Vue文件中的script部分使用TypeScript
  {
    files: ['**/*.vue'],
    languageOptions: {
      parserOptions: {
        parser: typescriptParser,
        ecmaVersion: 'latest',
        sourceType: 'module'
      }
    }
  },

  // 忽略文件
  {
    ignores: [
      'dist/**',
      'node_modules/**',
      '.nuxt/**',
      '.output/**',
      '.vite/**',
      'auto-imports.d.ts',
      'components.d.ts',
      '*.min.js'
    ]
  }
]
